﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIA.Configurators;
using System.Configuration;

namespace SIA.Configurators.xml
{
    public class XmlDictionaryProperty : AbstractDictionaryProperty
    {
        private String _directory;
        private System.IO.FileSystemWatcher _fsw;
        public String Directory { get { return _directory; } }

        public XmlDictionaryProperty(String directory)
        {
            if (!System.IO.Directory.Exists(directory)) throw new System.IO.DirectoryNotFoundException();

            this._directory = directory;

            load();


            _fsw = new System.IO.FileSystemWatcher(_directory, "*.config");
            _fsw.Created += new System.IO.FileSystemEventHandler(_fsw_Created);
            _fsw.Deleted += new System.IO.FileSystemEventHandler(_fsw_Deleted);
            _fsw.Renamed += new System.IO.RenamedEventHandler(_fsw_Renamed);
            _fsw.EnableRaisingEvents = true;
        }

        void _fsw_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            this.Remove(e.OldName);
            this.Add(e.Name, new XmlFileDictionaryProperty(e.FullPath));
        }

        void _fsw_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            this.Remove(e.Name);
        }

        void _fsw_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            Add(e.Name, new XmlFileDictionaryProperty(e.FullPath));
        }

        public override void load()
        {
            this.Clear();

            foreach (String str in System.IO.Directory.GetFiles(_directory, "*.config", System.IO.SearchOption.AllDirectories))
            {
                String key = str.Substring(str.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1);
                if (!this.ContainsKey(key))
                    Add(key, new XmlFileDictionaryProperty(str));
            }
        }


    }





    public class XmlFileDictionaryProperty: AbstractDictionaryProperty
    {
        private String _nomeFile;
        private Configuration _configFile;

        public XmlFileDictionaryProperty(String nomeFile)
        {
            this._nomeFile = nomeFile;

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = _nomeFile;
            _configFile = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            this.load();
        }

        public override void load()
        {
            foreach (String key in _configFile.AppSettings.Settings.AllKeys)
                this.Add(key, new SimpleDictionaryProperty(key, _configFile.AppSettings.Settings[key].Value));
        }

        public new IDictionaryProperty this[String key] {
            get
            {
                if (base.ContainsKey(key) ) return base[key];
                return new SimpleDictionaryProperty(key, _configFile.AppSettings.Settings[key].Value);                
            }
        }
    }
}
